from fastapi import APIRouter
from ..model.infer import ScpJob, AOI, Lot
from ..utils.database import InferSessionLocal
from ..utils.post_item import AddAOIItem, DeleteAOIItem, UserAllItem
from sqlalchemy import func
router = APIRouter()


# 获取默认参数
@router.get('/job')
async def job():
    res = []
    with InferSessionLocal() as session:
        jobs = session.query(ScpJob).order_by(ScpJob.start_datetime.desc()).all()
        for job in jobs:
            item = {}
            item["job_code"] = job.job_code
            aoi_code = job.aoi_code
            lot_code = job.lot_code

            aoi = session.query(AOI).filter_by(aoi_code=aoi_code).first()
            lot = session.query(Lot).filter_by(lot_code=lot_code).first()

            if aoi:
                item["aoi_name"] = aoi.aoi_name
            if lot:
                item["lot_name"] = lot.lot_name
            item["start_datetime"] = job.start_datetime.strftime('%Y-%m-%d %H:%M')
            item["end_datetime"] = job.end_datetime.strftime('%Y-%m-%d %H:%M')
            item["status"] = job.status
            res.append(item)

    return res


# 获取 aoi 服务器数据列表
@router.post('/aoi/all')
async def all_aoi(item: UserAllItem):
    res = []
    page_index = item.page_index
    page_size = item.page_size
    offset_data = page_size * (page_index-1)
    with InferSessionLocal() as session:
        total_num = session.query(func.count(AOI.aoi_code)).scalar()
        aois = session.query(AOI).offset(offset_data).limit(page_size).all()
        for aoi in aois:
            aoi_item = {}
            aoi_item["hostname"] = aoi.hostname
            aoi_item["port"] = aoi.port
            aoi_item["username"] = aoi.username
            aoi_item["password"] = aoi.password
            aoi_item["lotdir"] = aoi.lotdir
            aoi_item["server_host"] = aoi.server_host
            aoi_item["aoi_name"] = aoi.aoi_name
            aoi_item["aoi_code"] = aoi.aoi_code

            res.append(aoi_item)
    
    return {"data": res, "total_num": total_num}


# 添加 aoi 服务器数据
@router.post('/aoi/add')
async def add_aoi(item: AddAOIItem):
    hostname = item.hostname
    port = item.port
    username = item.username
    password = item.password
    lotdir = item.lotdir
    aoi_name = item.aoi_name

    with InferSessionLocal() as session:
        aoi = session.query(AOI).filter_by(hostname=hostname).first()
        if aoi:
            return {"code": 1, "msg": "AOI hostname 已存在"}
        aoi = AOI(hostname=hostname, port=port, username=username, password=password, lotdir=lotdir, aoi_name=aoi_name)
        session.add(aoi)
        session.commit()

    return {"code": 0, "msg": "添加 aoi 服务器成功"}


# 修改 aoi 服务器数据
@router.post('/aoi/modify')
async def modify_aoi(item: AddAOIItem):
    hostname = item.hostname
    port = item.port
    username = item.username
    password = item.password
    lotdir = item.lotdir
    aoi_name = item.aoi_name
    aoi_code = item.aoi_code

    with InferSessionLocal() as session:
        aoi = session.query(AOI).filter_by(aoi_code=aoi_code).first()
        
        aoi.hostname=hostname
        aoi.port=port
        aoi.username=username
        aoi.password=password
        aoi.lotdir=lotdir
        aoi.aoi_name=aoi_name
        session.commit()

    return {"code": 0, "msg": "修改 aoi 服务器成功"}


# 删除 aoi 服务器数据
@router.post('/aoi/delete')
async def delete_aoi(item: DeleteAOIItem):
    hostname = item.hostname

    with InferSessionLocal() as session:
        aoi = session.query(AOI).filter_by(hostname=hostname).first()
        session.delete(aoi)
        session.commit()

    return {"code": 0, "msg": "删除 aoi 服务器成功"}

            
            

